package com.biz.hunter.dao;

import com.biz.hunter.db.entity.*;
import com.biz.hunter.dbutil.BaseDAO;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;


@Repository
public class PersonDAO {
    protected static final String SQL_SAVE = "insert into tb_person(openid,name,age,workage,"
            + "phone,address,email,icon,"
            + "image,bestdomain,active,createDate,"
            + "inservice,companyID,  cityId, roleId,"
            + "companyname, mobile, platformid,domainId,createby,"
			+ "cityName, roleName) "
            + "values(?,?,?,?"
            + ",?,?,?,?"
            + ",?,?,?,?"
            + ",?,?,?,?"
            + ",?,?,?,?"
			+ ",?,?,?)";

    protected static final String SQL_UPDATE = "update tb_person set openid=?, name=?" +
            ",age=?, workage=?,phone=?,address =?,email=?,icon=?,image=?,bestdomain=? "
            + ", active=?, createDate=? ,inservice=?,companyID=?, cityid=?, roleid=?, companyname=?, mobile=?, "
            + "roleName = ?, platformid=? where id =?";


    protected static final String SQL_GET_ALL = "select * from tb_person ";

    protected static final String SQL_GET_BY_PHONE = "select * from tb_person where phone = ? limit 0,1";

    protected static final String SQL_GET_LIST = "select * from tb_person where id in (";

    protected static final String SQL_GET = "select * from tb_person  where id = ?";

    protected static final String SQL_DELETE = "delete from tb_person where id=?";

    public int save(Person person) {
        if (person == null) {
            return -1;
        }
        Long id = BaseDAO.save(SQL_SAVE, person.getOpenid(),
                person.getName(), person.getAge(), person.getWorkage(), person.getPhone(),
                person.getAddress(), person.getEmail(), person.getIcon(), person.getImage(),
                person.getBestdomain(), person.getActive(), person.getCreateDate(),
                person.getInservice(), person.getCompanyId(), person.getCityId(), person.getRoleId(),
                person.getCompanyName(), person.getMobile(), person.getPlatformId(),
                person.getDomainId(), person.getCreateby(), person.getCityName(), person.getRoleName()
        );

        return id.intValue();
    }

    public int delete(String... personIds) {
        if (personIds != null) {
            if (personIds.length == 1) {
                return BaseDAO.execute(SQL_DELETE, personIds[0]);
            } else {
                Object[][] params = new Object[personIds.length][1];
                int i = 0;
                for (String id : personIds) {
                    params[i][0] = id;
                }

                int[] rows = BaseDAO.batch(SQL_DELETE, params);
                if (rows != null && rows.length > 0) {
                    int total = 0;
                    for (int r : rows) {
                        total += r;
                    }
                    return total;
                } else {
                    return -1;
                }
            }
        } else {
            return 0;
        }
    }

    public List<Person> get(String... Ids) {
        if (Ids != null) {
            StringBuilder sql = new StringBuilder(SQL_GET_LIST);
            boolean start = false;
            for (String id : Ids) {
                if (start) {
                    sql.append(",").append(id);
                } else {
                    start = true;
                    sql.append(id);
                }
            }
            sql.append(")");
            return BaseDAO.queryForList(Person.class, sql.toString());

        } else {
            return Collections.emptyList();
        }
    }

    public List<Person> getALL() {
        return BaseDAO.queryForList(Person.class, SQL_GET_ALL);
    }

    protected static final String SQL_UPDATE1 = "update tb_person set openid=?, name=?" +
            ",age=?, workage=?,phone=?,address =?,email=?,icon=?,image=?,bestdomain=? ,"
            + " active=?, createDate=? ,inservice=?, companyID=?, companyname=?, cityid=?,cityname= ?,"
            + " roleid=?, roleName=? , mobile=?, platformid=? where id =?";

    public int update(Person person) {
        return BaseDAO.execute(SQL_UPDATE1, person.getOpenid(), person.getName(), person.getAge(),
                person.getWorkage(), person.getPhone(), person.getAddress(), person.getEmail(),
                person.getIcon(), person.getImage(), person.getBestdomain(),
                person.getActive(), person.getCreateDate(), person.getInservice(),
                person.getCompanyId(), person.getCompanyName(), person.getCityId(), person.getCityName(),
                person.getRoleId(), person.getRoleName(),
                person.getMobile(), person.getPlatformId(), person.getId());
    }

    protected static final String SQL_UPDATE2 = "update tb_person set  name=?" +
            ",age=?, workage=?,"
            + " inservice=?, companyID=?, companyname=?, cityid=?,cityname= ?,"
            + " roleid=?, roleName=?, createby=?, cityName=?, roleName=?  where phone =?";

    public int updateCandidate(Person person) {
        return BaseDAO.execute(SQL_UPDATE2, person.getName(), person.getAge(),
                person.getWorkage(), person.getInservice(),
                person.getCompanyId(), person.getCompanyName(), person.getCityId(), person.getCityName(),
                person.getRoleId(), person.getRoleName(), person.getCreateby(), person.getCityName(), person.getRoleName(), person.getPhone());
    }


    public Person getById(int personId) {
        return BaseDAO.queryForObject(Person.class, SQL_GET, personId);
    }

    public Person getByPhone(String phone) {
        return BaseDAO.queryForObject(Person.class, SQL_GET_BY_PHONE, phone);
    }

    public int delete(int personId) {
        return BaseDAO.execute(SQL_DELETE, personId);
    }


    protected static final String SQL_UPDATE_PROFILE = "update tb_person "
            + " set  name=? ,  workage=?, bestdomain=? ,   "
            + "companyId=?, companyname=?, cityId=?,cityName= ?,"
            + " roleId=?, roleName=? , platformid=?,  domainId= ? , domainName=? ,image=? where id =?";

    public int updateProfile(Person person) {
        int execute = BaseDAO.execute(SQL_UPDATE_PROFILE, person.getName(),
                0, person.getBestdomain(),
                person.getCompanyId(), person.getCompanyName(), person.getCityId(),
                person.getCityName(), person.getRoleId(), person.getRoleName(),
                person.getPlatformId(), person.getDomainId(), person.getDomainName(), person.getImage(),
                person.getId());
        System.out.println("[updateProfile SQL]: " + SQL_UPDATE_PROFILE + "  rows:" + execute);
        return execute;
    }

    protected static final String SQL_GET_EMP_ID = "select * from tb_person  where id = ?";

    public EmployeePerson getByEmpId(int personId) {
        return BaseDAO.queryForObject(EmployeePerson.class, SQL_GET_EMP_ID, personId);
    }

    private static final String SQL_GET_UNIONID = "select c.openid as openid from tb_person a,userinfo b,tb_formid c where a.openid=b.openid and b.unionid=c.unionid and a.id=? and c.wxapptype=? limit 0,1";

    public UserInfo getOpenidByUnionid(int personid) {
        return BaseDAO.queryForObject(UserInfo.class, SQL_GET_UNIONID, personid, TbForm.zhidao);
    }


    private static final String SAVE_RECOM = "INSERT INTO `tb_person_recom` (\n" +
            "\t`openid`,\n" +
            "\t`name`,\n" +
            "\t`age`,\n" +
            "\t`workage`,\n" +
            "\t`phone`,\n" +
            "\t`address`,\n" +
            "\t`email`,\n" +
            "\t`icon`,\n" +
            "\t`image`,\n" +
            "\t`createdate`,\n" +
            "\t`companyId`,\n" +
            "\t`companyname`,\n" +
            "\t`domainId`,\n" +
            "\t`domainName`,\n" +
            "\t`roleId`,\n" +
            "\t`roleName`,\n" +
            "\t`active`,\n" +
            "\t`inservice`,\n" +
            "\t`cityId`,\n" +
            "\t`cityName`,\n" +
            "\t`mobile`,\n" +
            "\t`platformid`,\n" +
            "\t`bestdomain`,\n" +
            "\t`comment`,\n" +
            "\t`intentTime`\n" +
            ") values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    public int saveRecom(PersonRecom person) {
        return BaseDAO.execute(SAVE_RECOM, person.getOpenid(), person.getName(), person.getAge(), person.getWorkage(),
                person.getPhone(), person.getAddress(), person.getEmail(), person.getIcon(), person.getImage(), System.currentTimeMillis(),
                person.getCompanyId(), person.getCompanyName(), person.getDomainId(), person.getDomainName(), person.getRoleId(),
                person.getRoleName(), person.getActive(), person.getInservice(), person.getCityId(), person.getCityName(),
                person.getMobile(), person.getPlatformId(), person.getBestdomain(), person.getComment(), person.getIntentTime());
    }

    public static final String GET_RECOM_ID = "select * from tb_person_recom where id = ?";

    public PersonRecom getRecomById(Integer id) {
        return BaseDAO.queryForObject(PersonRecom.class, GET_RECOM_ID, id);
    }

    public static final String UPDATE_PHONE = "update tb_employee set phone = ? where id =  ?";

    public int updateEmpPhone(String phone, Integer id) {
        return BaseDAO.execute(UPDATE_PHONE, phone, id);
    }
}
